package com.mingqijia.gassafety.authority.security.facade;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mingqijia.gassafety.authority.api.req.department.CreateDepartmentReqDTO;
import com.mingqijia.gassafety.authority.api.req.department.QueryDepartmentReqDTO;
import com.mingqijia.gassafety.authority.api.req.department.UpdateDepartmentReqDTO;
import com.mingqijia.gassafety.authority.api.resp.department.DepartmentRespDTO;
import com.mingqijia.gassafety.authority.api.web.facade.DepartmentManageFacade;
import com.mingqijia.gassafety.authority.dal.command.RemoveDepartmentCommand;
import com.mingqijia.gassafety.authority.dal.command.UpdateDepartmentCommand;
import com.mingqijia.gassafety.authority.dal.handler.HierarchyTranslator;
import com.mingqijia.gassafety.authority.dal.provider.CompanyProvider;
import com.mingqijia.gassafety.authority.dal.provider.HierarchyProvider;
import com.mingqijia.gassafety.db.entity.auth.CompanyEntry;
import com.mingqijia.gassafety.db.entity.auth.HierarchyEntry;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.enums.RunningState;
import com.mingqijia.gassafety.shared.facade.BaseFacade;
import com.mingqijia.gassafety.shared.handler.GlobalAuthenticationException;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;

/**
 * REST接口
 * @author Damon S.
 * @version v0.2
 * @date 2021-06-19
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class DepartmentManageFacadeImpl extends BaseFacade implements DepartmentManageFacade {

    private final HierarchyProvider provider;
    private final CompanyProvider cProvider;
    private final HierarchyTranslator translator;


    @Override
    public PageUtil<DepartmentRespDTO> findDepartment(String usage) {
        List<DepartmentRespDTO> departments = provider.findHierarchy(usage);

        // 返回所有部门,且带上级公司
        if (!Objects.equals(usage, Constants.DEPARTMENT)) {
            CompanyEntry entry = cProvider.getById(getHeaderSpId());
            DepartmentRespDTO root = translator.toRespDTO(entry);
            root.setChildren(departments);
            departments = Collections.singletonList(root);
        }
        return PageUtil.init(
                new Page<>(Constants.LONG_ONE,
                        Constants.LONG_1_KILO,
                        departments.size()),
                departments
        );
    }

    @Override
    public PageUtil<DepartmentRespDTO> findDepartment(@Valid QueryDepartmentReqDTO req) {
        Page<HierarchyEntry> results = provider.find(translator.toCommand(req));
        List<DepartmentRespDTO> departments = translator.toRespDTO(results.getRecords());

        provider.buildDisplay(departments);
        return PageUtil.init(results, departments);
    }

    @Override
    public String createDepartment(CreateDepartmentReqDTO req) {
        this.validateWorkingSpace4Enterprise();
        return provider.create(translator.toCommand(req));
    }

    @Override
    public void updateDepartment(UpdateDepartmentReqDTO req) {
        this.validateWorkingSpace4Enterprise();
        if (!provider.update(translator.toCommand(req))) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }

    @Override
    public void removeDepartment(String[] orgId) {
        this.validateWorkingSpace4Enterprise();
        RemoveDepartmentCommand command = RemoveDepartmentCommand.builder()
                .orgIds(Arrays.asList(orgId)).build();
        if (!provider.remove(command)) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }

    @Override
    public void switchDepartment(String orgId, Integer state) {
        this.validateWorkingSpace4Enterprise();
        if (state > Constants.INT_ONE || state < Constants.INT_ZERO) {
            throw new GlobalAuthenticationException("请求参数不合法");
        }
        UpdateDepartmentCommand command = UpdateDepartmentCommand.builder()
                .orgId(orgId).state(RunningState.parse(state)).build();
        if (!provider.update(command)) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }
}
